package atom.scenarios 
{
	import atom.message.Message;
	import atom.ui.components.Block;
	import atom.ui.components.stack.HorizontalStacker;
	import atom.ui.View;
	import com.greensock.easing.Quad;
	import com.greensock.TweenLite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author Jorge Braccini
	 */
	public class Scenario1 extends View {
		
		private var _tf:TextField;
		private var _temp:Object = { num: 0 }
		private var _container_a:HorizontalStacker;
		private var _container_b:HorizontalStacker;
		
		public function Scenario1(init:Object = null) {
			super(init);
		}
		
		override protected function create():void {
			super.create();
			
			link.addListener("request", handleResponse);
			stage.addEventListener(MouseEvent.CLICK, sendMessage);
			
			_container_a = new HorizontalStacker({center: true});
			_container_b = new HorizontalStacker();
			
			for (var i:int = 0; i < 50; i++) {
				_container_a.addView(new Block( { 	width: 	50, 
													height: 50,
													color: 	(Math.random() * 0xff) << 8 } ));
												
				_container_b.addView(new Block( { 	width:  50, 
													height: 50,
													color: 	Math.random() * 0xff } ));
			}
			
			addView(_container_a);
			addView(_container_b);
			
			resize();
			
			stage.addEventListener(Event.RESIZE, handleResize);
			stage.addEventListener(MouseEvent.CLICK, handleClick);
		}
		
		private function resize():void {
			_container_a.setSize(stage.stageWidth / 2, stage.stageHeight);
			_container_b.setSize(stage.stageWidth / 2, stage.stageHeight);
			_container_b.x = stage.stageWidth / 2;
		}
		
		private function handleClick(e:MouseEvent):void { 
			//var o:Number = (_container.margin != 0) ? 0 : 20;
			//TweenLite.to(_container, .5, { margin: o, ease: Quad.easeInOut } );
		}
		
		private function handleResize(e:Event):void {
			resize();
		}
		
		private function sendMessage(e:MouseEvent = null):void {
			var m:Message  	= new Message();
				m.type 		= "request";
				m.data 		= _temp;
			
			link.send(m);
		}
		
		private function handleResponse(m:Message):void {
			var s:String = "";
			for (var p:String in m.data) s += p + ": " + m.data[p] + "\n"
			
			_temp = m.data;
			trace(s);
		}
		
		private function randRange(a:Number, b:Number):Number {
			return a + (Math.random() * (b - a));
		}
		
	}

}